
#include <BAction>
#include "member_BAction.h"

using namespace BWE;

#define member					(*(member_BAction*)_ptr)
#define member_allocate()		_ptr = new member_BAction(this)
#define member_release()		delete (member_BAction*)_ptr

BAction::BAction()
{
	member_allocate();
}
BAction::BAction(BWidget* widget)
{
	member_allocate();
	setWidget(widget);
}
BAction::BAction(BLayout* layout)
{
	member_allocate();
	setLayout(layout);
}
BAction::BAction(const BString& text, bool checkable)
{
	member_allocate();
	setText(text);
	setCheckable(checkable);
}
BAction::BAction(BIcon* icon, const BString& text, bool checkable)
{
	member_allocate();
	setIcon(icon);
	setText(text);
	setCheckable(checkable);
}
BAction::~BAction()
{
	member_release();
}

void BAction::setText(const BString& text)
{
	if(member.text != text)
	{
		member.text = text;
		member.dirty = true;
		emit(Signal_TextChanged, member.text);
		if (member.host)
			member.host->fresh();
	}
}
const BString& BAction::text() const
{
	return member.text;
}

void BAction::setIcon(BIcon* icon)
{
	if (member.icon != icon)
	{
		member.icon = icon;
		member.dirty = true;
		emit(Signal_ImageChanged, member.icon);
		if (member.host)
			member.host->fresh();
	}
}
BIcon* BAction::icon()
{
	return member.icon.ptr();
}
const BIcon* BAction::icon() const
{
	return member.icon.ptr();
}

void BAction::setCheckable(bool checkable)
{
	if(member.checkable != checkable)
	{
		member.checkable = checkable;
		member.checked = false;
		member.dirty = true;
		if (member.host)
			member.host->fresh();
	}
}
bool BAction::checkable() const
{
	return member.checkable;
}

void BAction::setChecked(bool checked)
{
	if(member.checkable && member.checked != checked)
	{
		member.checked = checked;
		member.dirty = true;
		if (member.host)
			member.host->fresh();
		emit(Signal_Checked, member.checked);
	}
}
bool BAction::checked() const
{
	return member.checked;
}

void BAction::setWidget(BWidget* widget)
{
	if (member.widget == widget)
		return;
	if (member.widget && member.host)
	{
		member.host->removeChild(member.widget);
	}
	member.widget = widget;
	member.dirty = true;
	if (member.widget && member.host)
	{
		member.host->addChild(member.widget);
	}
	if (member.host)
		member.host->fresh();
}
BWidget* BAction::widget() const
{
	return member.widget;
}

void BAction::setLayout(BLayout* layout)
{
	if (member.layout == layout)
		return;
	if (member.layout && member.host)
	{
		member.host->removeLayout(member.layout);
	}
	member.layout = layout;
	member.dirty = true;
	if (member.layout && member.host)
	{
		member.layout->setHost(member.host);
	}
	if (member.host)
		member.host->fresh();
}
BLayout* BAction::layout() const
{
	return member.layout;
}

const BRect& BAction::rect() const
{
	return member.rect;
}
void BAction::trigger()
{
	emit(Signal_Trigger);
}
